﻿Imports System.Data.OleDb
Public Class frmDeleteAccount

#Region "NotifyIcon"

    Private Sub frmDeleteAccount_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Try
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Visible = False
                NotifyIcon.Visible = True
                NotifyIcon.ShowBalloonTip(1, "SkyCoffee", "SkyCoffee is Still Running!", ToolTipIcon.Info)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub NotifyIcon_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon.MouseDoubleClick
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuLaunchApplication_Click(sender As Object, e As EventArgs) Handles mnuLaunchApplication.Click
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuExitApplication_Click(sender As Object, e As EventArgs) Handles mnuExitApplication.Click
        Application.Exit()
    End Sub
#End Region

    Dim ds As New DataSet()
    Dim intCurrentIndex As Integer = 0
    Dim da As New OleDbDataAdapter()
    Dim conn As New OleDbConnection()

    Dim strCurrentUsername As String
    Dim strCurrentPassword As String
    Dim strVerifyUsername As String
    Dim strVerifyPassword As String

    Private Sub frmDeleteAccount_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Loads frmAccountMenu
        pnlIcons.Controls.Clear()
        pnlIcons.Controls.Add(frmAccountMenu.pnlIcons)

        'Load ToolTips
        AccountToolTips()

        'Delete from database
        conn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;" & _
                "Data Source=|DataDirectory|\SkyCoffee.mdb"

        da.SelectCommand = New OleDbCommand("SELECT * FROM tblAccounts")
        da.SelectCommand.Connection = conn

        da.DeleteCommand = New OleDbCommand("DELETE FROM tblAccounts WHERE fldStaffNumber = @fldStaffNumber")
        da.DeleteCommand.Connection = conn
        da.DeleteCommand.Parameters.Add("@fldStaffNumber", OleDbType.Numeric, 6, "fldStaffNumber")

        da.Fill(ds)

        If ds.Tables(0).Rows.Count > 0 Then 'Check to see if the table is empty
            FillFields()
        Else
            MsgBox("No records exist in database")
        End If
    End Sub
    Public Sub FillFields()
        'Display data from tblAccounts
        txtStaffID.Text = ds.Tables(0).Rows(intCurrentIndex).Item("fldStaffNumber").ToString()
        strCurrentUsername = ds.Tables(0).Rows(intCurrentIndex).Item("fldStaffUsername").ToString()
        strCurrentPassword = ds.Tables(0).Rows(intCurrentIndex).Item("fldStaffPassword").ToString()
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        frmLogin.Show()
        Me.Close()
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Dim MsgBoxDelete As MsgBoxResult = MsgBox("Are You Sure You Wish To Delete This ?", MsgBoxStyle.YesNo)

        strVerifyUsername = txtUsername.Text
        strVerifyPassword = txtPassword.Text

        If ds.Tables(0).Rows.Count > 0 Then

            If String.Compare(strCurrentUsername, strVerifyUsername) Then
                MsgBox("You have not entered a valid username")
                txtUsername.Focus()
            ElseIf String.Compare(strCurrentPassword, strVerifyPassword) Then
                MsgBox("You have not entered a valid password")
                txtPassword.Focus()
            Else

                If MsgBoxDelete = MsgBoxResult.Yes Then

                    Dim dr As DataRow

                    dr = ds.Tables(0).Rows(intCurrentIndex)
                    dr.Delete() 'Delete the row

                    da.Update(ds)
                    ds.AcceptChanges()

                    MsgBox("Record removed successfully")

                Else
                    MsgBox("Record not removed")
                End If

            End If
        ElseIf ds.Tables(0).Rows.Count = 0 Then
            MsgBox("No records exist in database")
        End If
    End Sub

    Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
        If intCurrentIndex > 0 Then  'We move back only if we're not at the first row.
            intCurrentIndex = intCurrentIndex - 1  'Subtract one from the current index.
            FillFields()
        ElseIf ds.Tables(0).Rows.Count = 0 Then
            MsgBox("No records exist in database")
        Else
            MessageBox.Show("You are already at the first record.")
        End If
    End Sub

    Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
        If intCurrentIndex < ds.Tables(0).Rows.Count - 1 Then 'ds.Tables(0).Rows.Count - 1 is the index for the last row
            intCurrentIndex = intCurrentIndex + 1  'Add one to the current index.
            FillFields()
        ElseIf ds.Tables(0).Rows.Count = 0 Then
            MsgBox("No records exist in database")
        Else
            MessageBox.Show("You are already at the last record.")
        End If
    End Sub
End Class